package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Slf4j
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class TaskCompletedHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {
        //反序列化
        TaskCompleteContract taskCompleteContract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);
        if (taskCompleteContract == null || StringUtils.isBlank(taskCompleteContract.getInnerCode())) return;

        //投放工单，将售货机状态更改为运营状态
        if (taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_DEPLOY) {
            vendingMachineService.updateStatus(taskCompleteContract.getInnerCode(), VMSystem.VM_STATUS_RUNNING);
        }
        //撤机工单，将售货机状态更改为撤机状态
        if (taskCompleteContract.getTaskType() == VMSystem.TASK_TYPE_REVOKE) {
            vendingMachineService.updateStatus(taskCompleteContract.getInnerCode(), VMSystem.VM_STATUS_REVOKE);
        }

    }
}
